{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-07-03T15:24:08.300272Z",
     "start_time": "2025-07-03T15:24:08.291240Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "# 定义原始数据集\n",
    "data = {\n",
    "    '姓名': ['张三', '李四', '王五', '赵六'],\n",
    "    'X1_年龄': [25, 36, 45, 55],\n",
    "    'X2_收入': [10000, 50000, 80000, 100000]\n",
    "}\n",
    "\n",
    "# 提取特征数据（跳过姓名列）\n",
    "X1 = np.array(data['X1_年龄'], dtype=np.float64)  # 年龄特征\n",
    "X2 = np.array(data['X2_收入'], dtype=np.float64)  # 收入特征\n",
    "\n",
    "\n",
    "# 定义最小最大归一化函数\n",
    "def min_max_normalize(feature):\n",
    "    feature_min = np.min(feature)\n",
    "    feature_max = np.max(feature)\n",
    "    return (feature - feature_min) / (feature_max - feature_min)\n",
    "\n",
    "\n",
    "# 对年龄和收入分别进行归一化\n",
    "X1_normalized = min_max_normalize(X1)\n",
    "X2_normalized = min_max_normalize(X2)\n",
    "\n",
    "# 合并归一化结果到原始数据\n",
    "data_normalized = {\n",
    "    '姓名': data['姓名'],\n",
    "    'X1_年龄(归一化)': X1_normalized.round(4),  # 保留4位小数\n",
    "    'X2_收入(归一化)': X2_normalized.round(4)\n",
    "}\n",
    "\n",
    "# 打印归一化结果\n",
    "for key, value in data_normalized.items():\n",
    "    print(f\"{key}: {value}\")\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名: ['张三', '李四', '王五', '赵六']\n",
      "X1_年龄(归一化): [0.     0.3667 0.6667 1.    ]\n",
      "X2_收入(归一化): [0.     0.4444 0.7778 1.    ]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-03T15:25:36.815703Z",
     "start_time": "2025-07-03T15:25:36.800651Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "# 定义原始数据集\n",
    "data = {\n",
    "    '姓名': ['张三', '李四', '王五', '赵六'],\n",
    "    'X1_年龄': [25, 36, 45, 55],\n",
    "    'X2_收入': [10000, 50000, 80000, 100000]\n",
    "}\n",
    "\n",
    "# 提取特征数据（构建二维特征矩阵）\n",
    "features = np.array([data['X1_年龄'], data['X2_收入']]).T  # 形状为 (4, 2)\n",
    "\n",
    "# 初始化MinMaxScaler（默认映射到[0,1]，可通过feature_range参数自定义范围）\n",
    "scaler = MinMaxScaler()\n",
    "\n",
    "# 拟合数据并进行归一化转换\n",
    "features_normalized = scaler.fit_transform(features)\n",
    "\n",
    "# 合并归一化结果与原始姓名（保留4位小数）\n",
    "data_normalized = {\n",
    "    '姓名': data['姓名'],\n",
    "    'X1_年龄(归一化)': np.round(features_normalized[:, 0], 4),  # 第一列为年龄归一化结果\n",
    "    'X2_收入(归一化)': np.round(features_normalized[:, 1], 4)  # 第二列为收入归一化结果\n",
    "}\n",
    "\n",
    "# 打印归一化结果\n",
    "print(\"\\n归一化后特征矩阵（MinMaxScaler）:\")\n",
    "print(features_normalized)\n",
    "\n",
    "print(\"\\n带姓名的归一化结果:\")\n",
    "for name, age, income in zip(\n",
    "        data_normalized['姓名'],\n",
    "        data_normalized['X1_年龄(归一化)'],\n",
    "        data_normalized['X2_收入(归一化)']\n",
    "):\n",
    "    print(f\"{name}: 年龄={age}, 收入={income}\")\n"
   ],
   "id": "981d77f815e519c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "归一化后特征矩阵（MinMaxScaler）:\n",
      "[[0.         0.        ]\n",
      " [0.36666667 0.44444444]\n",
      " [0.66666667 0.77777778]\n",
      " [1.         1.        ]]\n",
      "\n",
      "带姓名的归一化结果:\n",
      "张三: 年龄=0.0, 收入=0.0\n",
      "李四: 年龄=0.3667, 收入=0.4444\n",
      "王五: 年龄=0.6667, 收入=0.7778\n",
      "赵六: 年龄=1.0, 收入=1.0\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "3d0acc059b03f3ce"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
